package flexbars.controls
{

//--------------------------------------
//  Events
//--------------------------------------

//--------------------------------------
//  Styles
//--------------------------------------

//--------------------------------------
//  Excluded APIs
//--------------------------------------

//--------------------------------------
//  Other metadata
//--------------------------------------

public class ExtendedCode39 extends Code39
{
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	public function ExtendedCode39()
	{
		super();
	}
	
	//--------------------------------------------------------------------------
	//
	//  Constants
	//
	//--------------------------------------------------------------------------
	
	private static const ASCII_CHARSET_REGEXP:RegExp = /^[\x00-\x7F]+$/;
	
	private static const CHARACTER_ENCODING:Array = 
	[
		"%U", "$A", "$B", "$C", "$D", "$E", "$F", "$G",
		"$H", "$I", "$J", "$K", "$L", "$M", "$N", "$O",
		"$P", "$Q", "$R", "$S", "$T", "$U", "$V", "$W",
		"$X", "$Y", "$Z", "%A", "%B", "%C", "%D", "%E",
		 " ", "/A", "/B", "/C", "/D", "/E", "/F", "/G",
		"/H", "/I", "/J", "/K", "/L",  "-",  ".", "/O",
		 "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
		 "8",  "9", "/Z", "%F", "%G", "%H", "%I", "%J",
		"%V",  "A",  "B",  "C",  "D",  "E",  "F",  "G",
		 "H",  "I",  "J",  "K",  "L",  "M",  "N",  "O",
		 "P",  "Q",  "R",  "S",  "T",  "U",  "V",  "W",
		 "X",  "Y",  "Z", "%K", "%L", "%M", "%N", "%O",
		"%W", "+A", "+B", "+C", "+D", "+E", "+F", "+G",
		"+H", "+I", "+J", "+K", "+L", "+M", "+N", "+O",
		"+P", "+Q", "+R", "+S", "+T", "+U", "+V", "+W",
		"+X", "+Y", "+Z", "%P", "%Q", "%R", "%S",
		function():String
		{
			return ["%T", "%X", "%Y", "%Z"][Math.floor(Math.random() * 4)];
		}
	];
	
	//--------------------------------------------------------------------------
	//
	//  Variables
	//
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//
	//  Overridden properties
	//
	//--------------------------------------------------------------------------
	
    //----------------------------------
    //  code
    //----------------------------------
	
	override public function set code(value:String):void
	{
		if( !ASCII_CHARSET_REGEXP.test(value) )
			throw new ArgumentError("ExtendedCode39 code value");
		
		super.code = value;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Properties
	//
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//
	//  Overridden methods
	//
	//--------------------------------------------------------------------------
	
    //----------------------------------
    //  encodeCharacter
    //----------------------------------
	
	override protected function encodeCharacter(character:String):void
	{
		var asciiCode:int = character.charCodeAt(0);
		var extendedCharacter:* = CHARACTER_ENCODING[asciiCode];
		
		// TODO change "is Function" into "is Array"
		if(extendedCharacter is Function)
			extendedCharacter = extendedCharacter();
		
		super.encodeCharacter( extendedCharacter.charAt(0) );
		
		if(extendedCharacter.length == 2)
			super.encodeCharacter( extendedCharacter.charAt(1) );
	}
	
	//--------------------------------------------------------------------------
	//
	//  Methods
	//
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//
	//  Overridden event handlers
	//
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//
	//  Event handlers
	//
	//--------------------------------------------------------------------------
	
}

}